package com.koron.device.service.impl;

import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.device.bean.Equipment;
import com.koron.device.bean.SLanguagesConvert;
import com.koron.device.mapper.EquipmentMapper;
import com.koron.device.mapper.SLanguagesConvertDao;
import com.koron.device.service.SLanguagesConvertService;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 多语言转换表(SLanguagesConvert)表服务实现类
 *
 * @author makejava
 * @since 2023-10-12 09:13:33
 */
@Service("sLanguagesConvertService")
public class SLanguagesConvertServiceImpl implements SLanguagesConvertService {
    @Resource
    private SLanguagesConvertDao sLanguagesConvertDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public List<SLanguagesConvert> queryById(String id) {
        SLanguagesConvert sLanguagesConvert = new SLanguagesConvert();
        sLanguagesConvert.setParamId(id);
        return sLanguagesConvertDao.queryAllByLimit(sLanguagesConvert);
    }


    /**
     * 新增数据
     *
     * @param sLanguagesConvert 实例对象
     * @return 实例对象
     */
    @Override
    public SLanguagesConvert insert(SLanguagesConvert sLanguagesConvert) {
        sLanguagesConvert.setCreateTime(new Date());
        sLanguagesConvertDao.insert(sLanguagesConvert);
        return sLanguagesConvert;
    }

    /**
     * 修改数据
     *
     * @param sLanguagesConvertList 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public boolean update(List<SLanguagesConvert> sLanguagesConvertList) {
        sLanguagesConvertList.stream().forEach(sLanguagesConvert -> {
            sLanguagesConvert.setUpdateTime(new Date());
            sLanguagesConvertDao.update(sLanguagesConvert);
        });
        return true;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.sLanguagesConvertDao.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public int init() {
        ADOConnection.runTask("water_02",sessionFactory -> {
            EquipmentMapper mapper = sessionFactory.getMapper(EquipmentMapper.class);
            List<Equipment> equipmentList = mapper.queryAll();
            List<SLanguagesConvert> sLanguagesConvertList = new ArrayList<>();
            Date date = new Date();
            equipmentList.stream().forEach(equipment -> {
                sLanguagesConvertList.addAll(buildSLanguagesConvert(equipment.getId(),equipment.getName(),date));
                if (sLanguagesConvertList.size()>=1000){
                    sLanguagesConvertDao.insertBatch(sLanguagesConvertList);
                    sLanguagesConvertList.clear();
                }
            });
            if (!CollectionUtils.isEmpty(sLanguagesConvertList)){
                return sLanguagesConvertDao.insertBatch(sLanguagesConvertList);
            }
            return 0;
        },Integer.class);
        return 0;
    }

    @Override
    @Transactional
    public int addBycreatEquip(String equipId,String equipName) {
        List<SLanguagesConvert> sLanguagesConvertList = buildSLanguagesConvert(equipId, equipName,new Date());
        return sLanguagesConvertDao.insertBatch(sLanguagesConvertList);
    }
    @Override
    public List<SLanguagesConvert> buildSLanguagesConvert(String equipId,String equipName,Date date) {
        List<SLanguagesConvert> sLanguagesConvertList = new ArrayList<>();
        Map<String, String> configByCode = ConfigInfoTool.getConfigByCode("common", "language_config");
        for (String key : configByCode.keySet()) {
            SLanguagesConvert sLanguagesConvert = new SLanguagesConvert();
            sLanguagesConvert.setId(UUID.randomUUID().toString());
            sLanguagesConvert.setParamId(equipId);
            sLanguagesConvert.setLangType(key);
            sLanguagesConvert.setCreateTime(date);
            sLanguagesConvert.setLangValue(equipName);
            sLanguagesConvertList.add(sLanguagesConvert);
        }
        return sLanguagesConvertList;
    }
}
